layui.use(["layer", "laypage"], function () {
    const layer = layui.layer;
    const laypage = layui.laypage;

    /** 新建项目 */
    $("#menu input").on("click", function () {
        window.location.href = "insertview";
    });

    $("#table1 tbody tr").each(function () {
        const id = $(this).attr("id");
        $(this).find("input:eq(0)").on("click", function () {
            window.location.href = "updateview?id=" + id;
        });

        // 提交项目
        $(this).find("input:eq(1)").on("click", function () {
            if (!confirm("Are you sure you want to submit this data?"))
                return false;
            $(this).css("background-color", "#CCC");
            $(this).attr("disabled", true);
            if (Ajax("submit", {id: id}, "GET"))
                layer.msg("Operation successful！", {icon: 1});
            setTimeout("window.location.reload()", 2000);
        });

        // 删除项目
        $(this).find("input:eq(2)").on("click", function () {
            if (!confirm("Are you sure you want to delete this data?"))
                return false;
            $(this).css("background-color", "#CCC");
            $(this).attr("disabled", true);
            if (Ajax("delete", {id: id}, "GET"))
                layer.msg("Operation successful！", {icon: 1});
            setTimeout("window.location.reload()", 2000);
        });
    });
    /** *************************************************************** */
    laypage.render({
        elem: "page",
        curr: $("#page").data("p1"),
        count: $("#page").data("p2"),
        limit: 15,
    });

    $("#page a").on("click", function () {
        let page = $(this).text();
        if (page === "上一页")
            page = Number($("#page").data("p1") - 1);
        if (page === "下一页")
            page = Number($("#page").data("p1") + 1);
        window.location.href = "showlist?page=" + page;
    });
    $(".layui-disabled").off("click");

    /** *************************************************************** */
    function Ajax(url, data, type) {
        let result = null;
        $.ajax({
            url: url,
            data: data,
            type: type,
            async: false,
            datatype: "json",
            success: function (data) {
                result = data;
            }
        });
        return result;
    }

});
